import random


def guess_game():
    print("=== 猜数字游戏 ===")

    # 难度设置
    levels = {
        "1": {"name": "简单", "max_num": 50, "tries": 10},
        "2": {"name": "普通", "max_num": 100, "tries": 10},
        "3": {"name": "困难", "max_num": 200, "tries": 10}
    }

    # 选择难度
    print("请选择难度：")
    print("1. 简单（1-50）")
    print("2. 普通（1-100）")
    print("3. 困难（1-200）")

    while True:
        choice = input("输入数字选择难度 (1/2/3): ")
        if choice in levels:
            config = levels[choice]
            break
        print("输入错误，请重新选择！")

    # 初始化游戏
    secret = random.randint(1, config["max_num"])
    remaining = config["tries"]

    print(f"\n{config['name']}模式开始！数字范围1-{config['max_num']}")
    print(f"你有{remaining}次机会，加油！")

    # 游戏循环
    for attempt in range(1, config["tries"] + 1):
        try:
            guess = int(input(f"\n第{attempt}次猜测: "))

            # 验证输入范围
            if guess < 1 or guess > config["max_num"]:
                print(f"请输入1到{config['max_num']}之间的数字！")
                continue

            # 判断结果
            if guess == secret:
                print(f"恭喜！第{attempt}次猜中了！")
                return
            elif guess < secret:
                print("猜小了！")
            else:
                print("猜大了！")

            # 显示剩余次数
            print(f"剩余机会: {config['tries'] - attempt}")

        except ValueError:
            print("请输入数字！")

    print(f"\n游戏结束，正确答案是：{secret}")


# 启动游戏
if __name__ == "__main__":
    guess_game()